package m.jclecai.com.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.okhttp.Request;

import java.io.IOException;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.ProofBean;
import m.jclecai.com.bean.UserBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.BaseUtils;
import m.jclecai.com.utils.MD5;
import m.jclecai.com.utils.MyToast;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.Validator;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;

/**
 * Created by ZD on 2017/5/22.
 */

public class BindIDCardActivity extends BaseActivity {

    UserBean userBean;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_idcard)
    TextView tvIdcard;
    @BindView(R.id.ll_show)
    LinearLayout llShow;
    @BindView(R.id.et_card)
    EditText etCard;
    @BindView(R.id.et_rcard)
    EditText etRcard;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.tv_sure)
    TextView tvSure;
    @BindView(R.id.ll_bind)
    LinearLayout llBind;
    @BindView(R.id.et_name)
    EditText etName;

    @Override
    public int getContentView() {
        return R.layout.activity_bind_idcard;
    }

    @Override
    public void initView() {
        super.initView();
        setTitle("身份证绑定");
        leftOptionText("");
    }

    @Override
    public void initData() {
        super.initData();
        userBean = PreferenceUtil.readUser(this);
        llBind.setVisibility(View.GONE);
        llShow.setVisibility(View.GONE);
        if (userBean != null && !TextUtils.isEmpty(userBean.getIdcard())) {
            tvName.setText(userBean.getRealname());
            tvIdcard.setText(BaseUtils.formatIdCard(userBean.getIdcard()));
            llShow.setVisibility(View.VISIBLE);
        } else {
            //绑定
            llBind.setVisibility(View.VISIBLE);
        }
    }

    //绑定身份证
    @OnClick(R.id.tv_sure)
    void bingCard() {
        final String name = etName.getText().toString();
        final String card = etCard.getText().toString();
        String rCrd = etRcard.getText().toString();
        String pwd = etPwd.getText().toString();
        if (TextUtils.isEmpty(name)) {
            MyToast.showToast(this, "请输入真实姓名");
            return;
        }
//        if (!Validator.isValidatedAllIdcard(card)) {
//            MyToast.showToast(this, "请输入正确的身份证号码");
//            return;
//        }
        if (!card.equals(rCrd)) {
            MyToast.showToast(this, "两次输入的身份证号码不匹配");
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            MyToast.showToast(this, "请输入登录密码");
            return;
        }
        showDialog();
        HttpManager.Param[] param = new HttpManager.Param[]{
            new HttpManager.Param("idcard", card),
                new HttpManager.Param("name", name),
                new HttpManager.Param("password", MD5.getMD5(pwd)),
                new HttpManager.Param("user", PreferenceUtil.getUserName(this))
        };
        HttpManager.postAsyn(Constant_Url.RESET_PASSWORD_URL, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                MyToast.showToast(BindIDCardActivity.this, getString(R.string.error_network));
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                ProofBean proofBean = FastOk.get(response, ProofBean.class);
                if (proofBean != null) {
                    MyToast.showToast(BindIDCardActivity.this, proofBean.getMessage());
                    if ("0".equals(proofBean.getStatus())) {
                        userBean.setRealname(name);
                        userBean.setIdcard(card);
                        PreferenceUtil.writeUser(BindIDCardActivity.this,userBean);
                        setResult(RESULT_OK);
                        finish();
                    }
                } else {
                    MyToast.showToast(BindIDCardActivity.this, getString(R.string.error_network));
                }
            }
        }, param);
    }

    public static  Intent getIntent(Context context){
        Intent intent = new Intent(context, BindIDCardActivity.class);
        return intent;
    }
}
